package com.his.his_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.his.his_system.domain.DzmHisDoctor;
import com.his.his_system.domain.DzmHisHospitalDoctorRelation;
import com.his.his_system.domain.DzmHisMember;
import com.his.his_system.mapper.DzmHisHospitalDoctorRelationMapper;
import com.his.his_system.mapper.DzmHisMemberMapper;
import com.his.his_system.service.DzmHisDoctorService;
import com.his.his_system.mapper.DzmHisDoctorMapper;
import com.his.his_system.vo.DoctorVo;
import com.his.his_system.vo.MemberAndDoctorVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author 赵康
 * @description 针对表【dzm_his_doctor(医生基本信息表)】的数据库操作Service实现
 * @createDate 2023-07-06 16:53:44
 */
@Service
public class DzmHisDoctorServiceImpl extends ServiceImpl<DzmHisDoctorMapper, DzmHisDoctor>
        implements DzmHisDoctorService {

    private static final long NOWTIME = System.currentTimeMillis() / 1000;
    @Autowired
    private DzmHisDoctorMapper mapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private DzmHisMemberMapper dzmHisMemberMapper;

    @Autowired
    private DzmHisHospitalDoctorRelationMapper dhMapper;

    @Autowired
    private DzmHisDoctorMapper doctorMapper;

    @Override
    public List<DoctorVo> selectAll() {
        return mapper.selectAll();
    }

    @Override
    public List<DoctorVo> selectLike(String keyWord) {
        return mapper.selectLike(keyWord);

    }

    //    首先在用户表插入
//    返回主键ID ，然后在医生表插入
    @Override
    @Transactional
    public Boolean add(MemberAndDoctorVo param) {

        DzmHisMember dzmHisMember = new DzmHisMember();
        dzmHisMember.setRank(getRid(param));
        dzmHisMember.setType(param.getType());
        dzmHisMember = setPswdAndUserName(param.getPhone(), dzmHisMember);
        dzmHisMember.setDepartmentId(param.getDepartmentId());
        Integer affact = dzmHisMemberMapper.addOne(dzmHisMember);
        Integer uid = dzmHisMember.getUid();

//        测试事务
//        String a =null;
//        a.toString();

//        医生表插入
        Date date = new Date();
        Long now = date.getTime() / 1000;
        DzmHisDoctor dzmHisDoctor = new DzmHisDoctor();
        dzmHisDoctor.setTrueName(param.getTrueName());
        dzmHisDoctor.setAge(param.getAge());
        dzmHisDoctor.setPhone(param.getPhone());
        dzmHisDoctor.setUid(uid);
        dzmHisDoctor.setCreateTime(now);
        dzmHisDoctor.setUpdateTime(now);
        dzmHisDoctor.setIntroduction("简介");
        return mapper.add(dzmHisDoctor);
    }

    @Override
    public MemberAndDoctorVo selById(Integer id) {
        return mapper.selById(id);
    }

    @Override
    @Transactional
    public Integer modify(MemberAndDoctorVo param) {
//    首先在用户表修改
        dzmHisMemberMapper.modify(param);
//    然后在医生表修改
//        param.setUpdateTime(NOWTIME);
        DzmHisDoctor dzmHisDoctor = new DzmHisDoctor();
        dzmHisDoctor.setPhone(param.getPhone());
        dzmHisDoctor.setTrueName(param.getTrueName());
        dzmHisDoctor.setStrong(param.getStrong());
        dzmHisDoctor.setAge(param.getAge());
        dzmHisDoctor.setIntroduction(param.getIntroduction());
        dzmHisDoctor.setId(param.getId());
        dzmHisDoctor.setUpdateTime(NOWTIME);
        return mapper.modify(dzmHisDoctor);
    }

    /**
     * 重置密码
     *
     * @param id 用户ID
     * @return
     */
    @Override
    @Transactional
    public boolean reset(Integer id) {
        DzmHisMember dzmHisMember = dzmHisMemberMapper.selectOneByUid(id);

        QueryWrapper<DzmHisDoctor> qr =
                new QueryWrapper<DzmHisDoctor>().eq("uid", dzmHisMember.getUid());
        DzmHisDoctor doctor = doctorMapper.selectOne(qr);
        dzmHisMember.setPassword(passwordEncoder.encode(doctor.getPhone().substring(5, 10)));
        return dzmHisMemberMapper.reset(dzmHisMember);
    }

    /**
     * 默认电话号码前六位当做账号，后六位当做密码
     *
     * @param param
     * @param dzmHisMember
     */
    private DzmHisMember setPswdAndUserName(String param, DzmHisMember dzmHisMember) {
        String pswd = param.substring(param.length() - 7, param.length() - 1);
        String encoded = passwordEncoder.encode(pswd);
        dzmHisMember.setUserName(param);
        dzmHisMember.setPassword(encoded);
        return dzmHisMember;
    }

    /**
     * 获取id
     *
     * @param param
     * @return
     */
    private Integer getRid(MemberAndDoctorVo param) {
        //        获取rank的ID
        DzmHisHospitalDoctorRelation dh = dhMapper.selectOne(new QueryWrapper<DzmHisHospitalDoctorRelation>()
                .select("rid").eq("title_level", param.getRank()));
        return dh.getRid();
    }

}




